package com.site.util;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.context.ApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

import com.site.dto.master.CodeBizBean;
import com.site.service.master.CodeService;
import com.site.util.constant.Constants;

/**
 * 
 * @author Lee 2013-02-25
 *
 */
public class CodeMapDBUtil {

	private CodeService codeService = null;
	
	public CodeService getCodeService() {
		return codeService;
	}
	public void setCodeService(CodeService codeService) {
		this.codeService = codeService;
	}

	private ApplicationContext springCtx = null;
	
	public Map<String, List<Code>> getAllCode(ServletContext context) {
		if(codeService==null)
		{
			springCtx=WebApplicationContextUtils.getWebApplicationContext(context);
			codeService=(CodeService)springCtx.getBean("codeService");
		}
		
		List<CodeBizBean> codeVoList = codeService.getAllCodeBizBean();
		Map<String, List<Code>> map = new HashMap<String, List<Code>>();
		List<Code> codeList = null;
		Code code = null;
		if(codeVoList.size()>0)
		{
			for(CodeBizBean codeVo:codeVoList)
			{
				if(map.size()==0)
				{
					codeList = new ArrayList<Code>();
					code = new Code();
					code.setCode(codeVo.getCodeKey());
					code.setText(codeVo.getCodeValue());
					code.setRemark(codeVo.getRemark());
					
					codeList.add(code);
					map.put(codeVo.getCodeName(), codeList);
				}else{
					if(map.containsKey(codeVo.getCodeName()))
					{
						code = new Code();
						code.setCode(codeVo.getCodeKey());
						code.setText(codeVo.getCodeValue());
						code.setRemark(codeVo.getRemark());
						map.get(codeVo.getCodeName()).add(code);
					}else{
						codeList = new ArrayList<Code>();
						code = new Code();
						code.setCode(codeVo.getCodeKey());
						code.setText(codeVo.getCodeValue());
						code.setRemark(codeVo.getRemark());
						
						codeList.add(code);
						map.put(codeVo.getCodeName(), codeList);
					}
				}
			}
		}
		
		return map;
	}
	

	/**
	 * 根据CodeName值获取一个codeList
	 * @param codeName
	 * @param request
	 * @return
	 */
	public static List<Code> getCodeByCodeName(String codeName,HttpServletRequest request) {
		Map<String, List<Code>> map = (Map<String, List<Code>>)request.getSession().getServletContext().getAttribute(Constants.CODE_MAP_NAME);
		if(map!=null&&map.size()>0) {
			return map.get(codeName);
		}
		return null;
	}
	
	/**
	 * 根据CodeName值获取一个codeList
	 * @param codeName
	 * @param servletContext
	 * @return
	 */
	public static List<Code> getCodeByCodeName(String codeName,ServletContext servletContext) {
		Map<String, List<Code>> map = (Map<String, List<Code>>)servletContext.getAttribute(Constants.CODE_MAP_NAME);
		if(map!=null&&map.size()>0)
		{
			return map.get(codeName);
		}
		return null;
	}

	/**
	 * 根据CodeName和键值获取CODE的单个值
	 * @param codeName
	 * @param key
	 * @param request
	 * @return
	 */
	public static String getCodeValueByKey(String codeName, String key,HttpServletRequest request) {
		Map<String, List<Code>> map = (Map<String, List<Code>>)request.getSession().getServletContext().getAttribute(Constants.CODE_MAP_NAME);
		if(map!=null&&map.size()>0)
		{
			List<Code> codeList = map.get(codeName);
			if(codeList!=null)
			{
				for(Code code:codeList)
				{
					if(code.getCode().equals(key))
						return code.getText();
				}
			}
		}
		return null;
	}
	
	
	/**
	 * 根据CodeName值获取一个codeList
	 * @param codeName
	 * @param session
	 * @return
	 */
	public static List<Code> getCodeByCodeNameSession(String codeName,HttpSession session) {
		if(session==null)
			return null;
		Map<String, List<Code>> map = (Map<String, List<Code>>)session.getServletContext().getAttribute(Constants.CODE_MAP_NAME);
		if(map!=null&&map.size()>0)
		{
			return map.get(codeName);
		}
		return null;
	}

	
	/**
	 * 根据CodeName和键值获取CODE的单个值
	 * @param codeName
	 * @param key
	 * @param session
	 * @return
	 */
	public static String getCodeValueByKeySession(String codeName, String key,HttpSession session) {
		if(session==null)
			return null;
		Map<String, List<Code>> map = (Map<String, List<Code>>)session.getServletContext().getAttribute(Constants.CODE_MAP_NAME);
		if(map!=null&&map.size()>0)
		{
			List<Code> codeList = map.get(codeName);
			if(codeList!=null)
			{
				for(Code code:codeList)
				{
					if(code.getCode().equals(key))
						return code.getText();
				}
			}
		}
		return null;
	}

}
